<template>
  <el-container class="home_container">
    <!-- 头部导航 -->
    <el-header>
      <div>
        <img src="../assets/school(1).png" alt="" />
        <span>校园报障系统</span>
      </div>

      <div>
        <el-tooltip
          class="item"
          effect="dark"
          content="个人信息"
          placement="bottom"
        >
          <el-avatar
            @click.native="showUser"
            class="iconDiv"
            icon="el-icon-user-solid"
          ></el-avatar>
        </el-tooltip>
        <el-button type="info" @click="logout">退出</el-button>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边导航 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="menu.id + ''"
            v-for="menu in menulist"
            :key="menu.id"
          >
            <template slot="title">
              <i :class="iconObj[menu.id]"></i>
              <span>{{ menu.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in menu.children"
              :key="subItem.id"
              @click="changeIndex('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>

    <el-drawer
      size="20%"
      title="我是标题"
      :visible.sync="drawer"
      :with-header="false"
    >
      <div class="block divAvatar">
        <el-avatar
          shape="circle"
          :size="150"
          src="http://47.107.64.149/group1/M00/00/01/rBY4rWHqp6KAfCJcAACpGiilOOE045.jpg"
        ></el-avatar>
      </div>
      <el-divider></el-divider>
      <div>
        <el-descriptions border :contentStyle="declass" :column="1">
          <el-descriptions-item
            label="编号:"
            :labelStyle="{ 'text-align': 'center' }"
            >{{ userForm.id }}</el-descriptions-item
          >
          <el-descriptions-item
            label="姓名:"
            :labelStyle="{ 'text-align': 'center' }"
            >{{ userForm.name }}</el-descriptions-item
          >
          <el-descriptions-item
            label="身份:"
            :labelStyle="{ 'text-align': 'center' }"
            ><el-tag size="medium">{{
              userForm.identity
            }}</el-tag></el-descriptions-item
          >
          <el-descriptions-item
            label="手机号码:"
            :labelStyle="{ 'text-align': 'center' }"
          >
            {{ userForm.phone }}
          </el-descriptions-item>
          <el-descriptions-item
            label="联系地址"
            :labelStyle="{ 'text-align': 'center' }"
            >{{ userForm.address }}</el-descriptions-item
          >
        </el-descriptions>
      </div>
    </el-drawer>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      declass: {
        "max-width": "100px",
      },
      menulist: [],
      iconObj: {
        1: "el-icon-setting",
        2: "iconfont icon-tijikongjian",
        3: "iconfont icon-shangpin",
        4: "iconfont icon-baobiao",
        5: "iconfont icon-user",
      },
      isCollapse: false,
      activePath: "",
      drawer: false,
      userForm: {},
    };
  },
  created() {
    this.getMenus();
    this.activePath = sessionStorage.getItem("activePath");
    //alert("改变了");
  },
  methods: {
    logout() {
      sessionStorage.clear();
      this.$router.push("/login");
      this.$message.success("已退出 !");
    },
    // 获取菜单栏数据
    async getMenus() {
      // const { data: res } = await this.$http.get('menus')
      // if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // this.menulist = res.data
      // console.log(res)[]
      this.menulist = [
        {
          id: 1,
          authName: "报修管理",
          children: [
            { id: 11, authName: "报修登记", path: "toreport" },
            { id: 150, authName: "我的报修", path: "evaluate" },
          ],
        },
        {
          id: 2,
          authName: "维修信息",
          children: [
            { id: 13, authName: "待分配", path: "allocation" },
            { id: 14, authName: "可抢单", path: "strived" },
            { id: 15, authName: "维修中", path: "repairing" },
            { id: 160, authName: "异常单", path: "errorbill" },
          ],
        },
        {
          id: 4,
          authName: "数据统计",
          children: [{ id: 199, authName: "统计图表", path: "total" }],
        },
        {
          id: 5,
          authName: "人员管理",
          children: [{ id: 200, authName: "维修员", path: "user" }],
        },
      ];
    },
    // 控制菜单栏切换隐藏
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 将index存入到sessionStorage中来保持菜单栏高亮
    changeIndex(activePath) {
      sessionStorage.setItem("activePath", activePath);
      this.activePath=activePath
    },

    async showUser() {
      const { data: res } = await this.$http.get("getUserDetail");
      if (res.meta.status !== 200) {
        return this.$message.console.error("个人信息获取失败");
      }
      this.userForm = res.data;
      this.drawer = true;
    },
  },
};
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.iconDiv {
  margin-right: 10px;
  background-color: #606266;
}

.divAvatar {
  margin-left: 22%;
  margin-top: 8%;
}

.my-label {
  text-align: center;
  background: #e1f3d8;
}
</style>
